package managedbean;


import java.io.Serializable;
import java.util.*;

import javax.ejb.EJB;
import javax.faces.bean.*;
import javax.naming.Context;
import javax.naming.InitialContext;

import jpa.OrderJPA;
import ejb.OrderFacadeRemote;


/**
 * Managed Bean TotalAmountByCustomerMBean
 */
@ManagedBean(name = "listOrders")
@SessionScoped
public class ListOrdersMBean implements Serializable {

	private static final long serialVersionUID = 1L;	
	
	@EJB
	private OrderFacadeRemote orderFR;
	private Collection<OrderJPA> ordersList;
	private int idOrder;
	
	public ListOrdersMBean() throws Exception   {
		ListOrders();
	}
		
	public Collection<OrderJPA> getOrdersList() {
		return ordersList;
	}
	
	public String ListOrders() throws Exception {
		FindAllOrders();
		return "OrderListView";
	}
	
	public void setIdOrder(int IdOrder) throws Exception {
		this.idOrder = IdOrder;
		ListOrders();
	}
	

	public int getIdOrder() {
		return idOrder;
	}

	@SuppressWarnings("unchecked")
	public Collection<OrderJPA> FindAllOrders() throws Exception {	
		Properties props = System.getProperties();
		Context ctx = new InitialContext(props);
		orderFR = (OrderFacadeRemote) ctx.lookup("java:app/PracticalCaseStudyJEE.jar/OrderFacadeBean!ejb.OrderFacadeRemote");
		ordersList = (Collection<OrderJPA>)orderFR.findAllOrders();
		Collections.sort((List<OrderJPA>) ordersList);
		return ordersList;
		
	}


	
}
